<template>
  <el-card>
    <el-row>
      <el-select style="margin-right: 20px" size="small" v-model="selectedCourseId" placeholder="请选择"
                 @change="currentSel">
        <el-option
          v-for="item in courseList"
          :key="item.courseId"
          :label="item.courseName"
          :value="item.courseId">
        </el-option>
      </el-select>
      <el-button type="primary" size="small" @click="getCourseExperimentList()">查询</el-button>
      <el-button type="success" @click="newExpFormVisibleFunc()" size="small">创建实验</el-button>
      <!--      <el-button type="primary" @click="getCourseExperimentList()" size="small">获取实验</el-button>-->
    </el-row>
    <el-row>
      <!--  展示老师选择的实验  -->
      <el-table :data="courseExperimentList" border stripe :default-sort="{prop: 'experId', order: 'ascending'}">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="experId" label="实验编码" width="180"></el-table-column>
        <el-table-column prop="experName" label="实验项目" width="180"></el-table-column>
        <!--scope相当于是tableData的一行，与el-table-column唯一对应-->
        <el-table-column prop="difficuty" label="实验难度" width="80px">
          <template v-slot:default="scope">
            <div v-if="scope.row.difficuty === 1">
              <el-tag type="success">基础</el-tag>
            </div>
            <div v-if="scope.row.difficuty === 2">
              <el-tag type="primary">提高</el-tag>
            </div>
            <div v-if="scope.row.difficuty === 3">
              <el-tag type="warning">综合</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="introduction" label="实验简介"></el-table-column>
        <!--        <el-table-column label="状态"></el-table-column>-->
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button @click="editExp(scope.row)" type="primary"
                         icon="el-icon-edit" size="small"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top"
                        :enterable="false">
              <el-button @click="deleteExp(scope.row)" type="danger"
                         icon="el-icon-delete"
                         size="small"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!--新建课程对话框-->
    <el-dialog title="创建实验" :visible.sync="newExpFormVisible" @close="addExpClosed">
      <el-form ref="newExpForm" :model="newExpForm" :rules="expFormRules">
        <el-form-item prop="courseId" size="small" label="课程名称" label-width="100px">
          <el-input v-model="this.CurrentCourseName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item prop="experId" size="small" label="实验编号" label-width="100px">
          <el-input size="small" v-model="newExpForm.experId" placeholder="请输入实验编号"></el-input>
        </el-form-item>
        <el-form-item prop="experName" size="small" label="实验名称" label-width="100px">
          <el-input size="small" v-model="newExpForm.experName" placeholder="请输入实验名称"></el-input>
        </el-form-item>
        <el-form-item prop="difficultyString" size="small" label="实验难度" label-width="100px">
          <el-select size="" v-model="newExpForm.difficultyString" placeholder="请选择实验难度">
            <el-option label="基础" value="1"></el-option>
            <el-option label="提高" value="2"></el-option>
            <el-option label="综合" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="introduction" label="实验简介" label-width="100px">
          <el-input type="textarea" autosize v-model="newExpForm.introduction" placeholder="请输入实验简介"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="newExpFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addExp">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data () {
    return {
      courseList: [],
      courseExperimentList: [],
      teacherExperimentList: [],
      CurrentSel: '',
      CurrentCourseName: '',
      selectedCourseId: '',
      obj: '',
      newExpForm: {
        courseId: '',
        experId: '',
        experName: '',
        difficultyString: '',
        difficuty: '',
        introduction: ''
      },
      newExpFormVisible: false,
      // 实验表单规则校验
      expFormRules: {
        experId: [
          {
            required: true,
            message: '请输入实验编号',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 10,
            message: '长度在 1 到 10 个字符',
            trigger: 'blur'
          }
        ],
        experName: [
          {
            required: true,
            message: '请输入实验名称',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 20,
            message: '长度在 1 到 20 个字符',
            trigger: 'blur'
          }
        ],
        introduction: [
          {
            required: true,
            message: '请输入实验简介',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 255,
            message: '长度在 1 到 255 个字符',
            trigger: 'blur'
          }
        ]
      },
      editExpForm: {
        courseId: '',
        experId: '',
        experName: '',
        difficuty: '',
        introduction: ''
      },
      editExpFormVisible: false
    }
  },
  async created () {
    await this.getCourseList()
    await this.getCourseExperimentList()
  },
  // computed: {
  //   // 使用计算属性 过滤数组数据
  //   // 也可以直接在getData中使用 filter 过滤
  //   courseExperimentListById: function () {
  //     return this.courseExperimentList.filter((data) => {
  //       return data.courseId === this.CurrentSel
  //     })
  //   }
  // },
  methods: {
    // 获取管理员创建的所有课程
    async getCourseList () {
      const { data: res } = await this.$http.post('/queryCourseList', { courseName: '' })
        .catch((error) => {
          console.log(error)
          this.$message.error('网络连接超时')
        })
      if (res.status !== 200) this.$message.error('获取课程信息失败')
      console.log(res.data.data)
      const courseList = res.data
      this.courseList = courseList.courses
      this.total = courseList.total
      this.selectedCourseId = this.courseList[0].courseId
    },
    // 由选中的课程获取课程对应的课程。
    currentSel (selVal) {
      this.CurrentSel = selVal
    },
    // 创建实验打开窗口，并获取选中课程的名字
    newExpFormVisibleFunc () {
      if (this.selectedCourseId !== '') {
        this.newExpFormVisible = true
        // 获取选中的课程名字
        this.obj = this.courseList.find((item) => {
          return item.courseId === this.selectedCourseId
        })
        this.CurrentCourseName = this.obj.courseName
        // console.log(this.CurrentCourseName)
      } else {
        this.$message.error('请先选择课程！')
      }
    },
    // 获取所有课程管理员已创建的实验，通过filter筛选不同courseId
    async getCourseExperimentList () {
      const { data: res } = await this.$http.post('/queryAllExperiment')
        .catch((error) => {
          console.log(error)
          this.$message.error('网络连接超时')
        })
      if (res.status !== 200) this.$message.error('获取实验信息失败,' + res.data.msg)
      // console.log(res)
      const courseExperimentList = res.data
      this.courseExperimentList = courseExperimentList.data.filter((data) => {
        return data.courseId === this.selectedCourseId
      })
    },
    // 创建实验
    async addExp () {
      // 选中的课程对应表单里的课程Id
      this.newExpForm.courseId = this.selectedCourseId
      this.newExpForm.difficuty = parseInt(this.newExpForm.difficultyString)
      // console.log(this.newExpForm)
      this.$refs.newExpForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/addExperiment', this.newExpForm)
          .catch((error) => {
            console.log(error)
          })
        if (res.status !== 200) {
          this.$message.error('创建实验失败,' + res.data.msg)
        } else {
          this.$message.success('创建实验成功')
          this.newExpFormVisible = false
          await this.getCourseExperimentList()
        }
      })
    },
    addExpClosed () {
      this.$refs.newExpForm.resetFields()
    },
    editExp (row) {
      this.editExpFormVisible = true
      // console.log(row)
      this.editExpForm = row
    },
    submitEditExp () {
      console.log(this.editCourseForm)
      this.$refs.editCourseForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/', this.editCourseForm)
          .catch((error) => {
            console.log(error)
          })
        if (res.status !== 200) {
          this.$message.error('更新实验失败,' + res.data.msg)
        } else {
          this.$message.success('更新实验成功')
          this.editCourseFormVisible = false
          await this.getCourseExperimentList()
        }
      })
    },
    async deleteExp (row) {
      console.log(row)
      const flag = await this.$confirm('确认要删除吗？')
      if (flag) {
        const { data: res } = await this.$http.post('/deleteExperiment', row)
          .catch((error) => {
            console.log(error)
            this.$message.error('删除实验失败,请检查网络')
          })
        if (res.status === 200) {
          this.$message.success('删除实验成功')
          await this.getCourseExperimentList()
        } else {
          this.$message.error(res.data.msg)
        }
      }
    }
  }
}
</script>
